const query = require('../public/connent.js')

module.exports = async (ctx, next) => {

  const {userName, userPwd} = ctx.request.body
  ctx.type = 'json'

  if (!userName || !userPwd) {
    ctx.body = {
      msg: '参数错误',
      res: 'FAIL',
      data: null
    }
  } else {
    let sql = `SELECT id, name, nickname, headimg From userinfo WHERE name='${userName}' AND pwd='${userPwd}'`
    const result = await query(sql)
    console.log(result)
    let msg
    if (result.length > 0) {
      msg = '查询成功'
      // 登录成功 设置登录状态
      query(`UPDATE userinfo SET loging=1 WHERE id=${result[0].id}`)
    } else {
      msg = '用户名或密码错误'
    }
    ctx.body = {
      msg,
      res: result.length ? 'SUCCESS' : 'FAIL',
      data: result.length? result[0] : null
    }
  }
}